package com.sskj.finance.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.finance.R;
import com.sskj.finance.R2;
import com.sskj.finance.bean.FinanRecordInfoBean;
import com.sskj.finance.presenter.MyOrderActivityPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.util.NumberUtil;
import com.sskj.lib.util.TimeFormatUtil;
import com.warkiz.widget.IndicatorSeekBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.FINAN_MY_ORDER)//我的订单
public class MyOrderActivity extends BaseActivity<MyOrderActivityPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    @Override
    protected int getLayoutId() {
        return R.layout.finan_activity_my_order;
    }

    @Override
    public MyOrderActivityPresenter getPresenter() {
        return new MyOrderActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.finan_myOrderActivity1_j));
        HashMap<String, String> statusMap = new HashMap<>();
        statusMap.put("1", App.INSTANCE.getString(R.string.finan_myOrderActivity2_j));
        statusMap.put("2", App.INSTANCE.getString(R.string.finan_myOrderActivity3_j));
        statusMap.put("3", App.INSTANCE.getString(R.string.finan_myOrderActivity4_j));
        statusMap.put("4", App.INSTANCE.getString(R.string.finan_myOrderActivity5_j));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.finan_recy_item_my_order, new SlimInjector<FinanRecordInfoBean>() {
            @Override
            public void onInject(FinanRecordInfoBean data, IViewInjector injector, List payloads) {
                injector
                        .text(R.id.tvName, data.getName())
                        .text(R.id.tvDay, data.getDays() + App.INSTANCE.getString(R.string.finan_myOrderActivity6_j))
                        .text(R.id.tvBuyNum, data.getNum())
                        .text(R.id.tvRate, (data.getStatus().equals("1") || data.getStatus().equals("2") ? NumberUtil.keepNoZero(data.getExpect_rate()) : NumberUtil.keep2(data.getReal_rate())) + "%")
                        .text(R.id.tvRateProfile, data.getExp_income())
                        .text(R.id.tvRateTitle, data.getStatus().equals("1") || data.getStatus().equals("2") ? App.INSTANCE.getString(R.string.finan_myOrderActivity7_j) : App.INSTANCE.getString(R.string.finan_myOrderActivity8_j))
                        .text(R.id.tvStatus, statusMap.get(data.getStatus()))
                        .with(R.id.indicatorSeekBar, view -> {
                            IndicatorSeekBar indicatorSeekBar = (IndicatorSeekBar) view;
                            indicatorSeekBar.setProgress(data.getRate());
                            indicatorSeekBar.setMin(0);
                            indicatorSeekBar.setMax(100);
                        })
                        .with(R.id.tvTime, view -> {
                            TextView tvTime = (TextView) view;
                            tvTime.setText(TimeFormatUtil.parseNewTime(data.getStart_time()));
                            Object tag = tvTime.getTag();
                            DisposUtil.close((Disposable) tag);

                            Disposable timeDispo = Flowable.interval(0, 1, TimeUnit.SECONDS)
                                    .map(aLong -> aLong + data.getStart_time())
                                    .compose(RxSchedulersHelper.transformer())
                                    .subscribe(aLong -> {
                                        tvTime.setText(TimeFormatUtil.parseNewTime(aLong));
                                    });
                            tvTime.setTag(timeDispo);
                        })
                        .visibility(R.id.tvTime, data.getStatus().equals("1") ? View.VISIBLE : View.INVISIBLE)
                ;
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<FinanRecordInfoBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<FinanRecordInfoBean>)
                page -> mPresenter.finanRecordInfo(page + "", "20"), 20);
        MVCCoolHelper<List<FinanRecordInfoBean>> mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

}
